﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PMS.Logic.BD
{
    public partial class GBAjax : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            String methodName = Request["method"];
            Type type = this.GetType();
            MethodInfo method = type.GetMethod(methodName);
            if (method == null) throw new Exception("method is null");
            try
            {
                method.Invoke(this, null);
            }
            catch
            {
                throw;
            }
        }

        /// <summary>
        /// 获取国标信息
        /// </summary>
        public void GetGBList()
        {
            List<SearchCondition> listSearch = new List<SearchCondition>();
            if (Request.Form["searchData"] != null)
            {
                string searchData = Request.Form["searchData"];
                listSearch = ModelTools.GetSearchContions(searchData);
            }
            Expression<Func<BD_GBInfo, bool>> exp = SearchExpression.FindByGroup<BD_GBInfo>(listSearch);
            string OrderId = OperateContext.Sort == null ? "ID" : OperateContext.Sort;
            int count = 0;
            var list = DALSession.Current.BD_GBInfoDAL.GetPagedList(OperateContext.Page, OperateContext.Rows, exp, OrderId, OperateContext.Order);
            count = DALSession.Current.BD_GBInfoDAL.GetCount(exp);
            Response.Write(JsonHelper.toPageJson(list, count));
        }


        /// <summary>
        /// 根据q获取国标信息
        /// </summary>
        public void GetGBInfoByQ()
        {
            string q = Request.Form["q"];
            var a = new List<BD_GBInfo>();
            List<SearchCondition> listSearch = new List<SearchCondition>();
            Expression<Func<BD_GBInfo, bool>> exp = SearchExpression.FindByGroup<BD_GBInfo>(listSearch);
            string OrderId = OperateContext.Sort == null ? "ID" : OperateContext.Sort;
            int count = 0;
            if (q == null)
            {
                var list = DALSession.Current.BD_GBInfoDAL.GetPagedList(OperateContext.Page, OperateContext.Rows, exp, OrderId, OperateContext.Order);
                count = DALSession.Current.BD_GBInfoDAL.GetCount(exp);
                Response.Write(JsonHelper.toPageJson(list, count));
            }
            else
            {
                SearchCondition search = new SearchCondition();
                search.Field = "GBCode";
                search.Value = q;
                search.Operator = "co";
                listSearch.Add(search);
                exp = SearchExpression.FindByGroup<BD_GBInfo>(listSearch);
                var list = DALSession.Current.BD_GBInfoDAL.GetPagedList(OperateContext.Page, OperateContext.Rows, exp, OrderId, OperateContext.Order);
                count = DALSession.Current.BD_GBInfoDAL.GetCount(exp);
                Response.Write(JsonHelper.toPageJson(list, count));
            }
        }


        public void GetGBInfoSX() {
            var list = DALSession.Current.BD_GBInfoDAL.GetListBy(li=>li.ID!=null);
            List<ComboList> res = new List<ComboList>();
            list.ForEach(li => { res.Add(new ComboList() {value=li.ID.ToString(), text = li.GBCode+"|"+li.GasName }); });
            Response.Write(JsonHelper.toJson(res));
            return;
        }

        /// <summary>
        /// 修改国标信息
        /// </summary>
        public void EditGBInfo()
        {
            int ID = Commons.String2Int32(Request.Form["ID"]);
            BD_GBInfo perNew = DALSession.Current.BD_GBInfoDAL.GetListBy(d => d.ID == ID).FirstOrDefault();

            ModelTools.Form2EditModel(Request.Form, perNew);
            DALSession.Current.BD_GBInfoDAL.ModifyAllModel(perNew);
            DALSession.Current.BD_GBInfoDAL.SaveChanges();
            Response.Write(JsonHelper.toDefaultMsg(true, "保存成功"));
        }
        /// <summary>
        /// 添加国标信息
        /// </summary>
        public void AddGBInfo()
        {
            BD_GBInfo per = new BD_GBInfo();
            ModelTools.Form2EditModel(Request.Form, per);
            DALSession.Current.BD_GBInfoDAL.Add(per);
            DALSession.Current.BD_GBInfoDAL.SaveChanges();
            Response.Write(JsonHelper.toDefaultMsg(true, "保存成功"));
        }
        /// <summary>
        /// 删除国标信息
        /// </summary>
        public void DelGBInfo()
        {
            //  int Id = Commons.String2Int32(Request.Form["Id"]);
            int Id = Convert.ToInt32(Request.Form["Id"].ToString());
            DALSession.Current.BD_GBInfoDAL.DelBy(p => p.ID == Id);
            DALSession.Current.BD_GBInfoDAL.SaveChanges();
            Response.Write(JsonHelper.toDefaultMsg(true, "删除成功"));
        }
    }
}